package com.lbn.monitor.app.warning;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.RadioGroup;

import com.free.framework.base.BaseFragmentPagerAdapter;
import com.free.framework.widgets.custom_loading.LoadingUtils;
import com.lbn.monitor.R;
import com.lbn.monitor.api.model.Pollutant;
import com.lbn.monitor.api.model.RealTimeValue;
import com.lbn.monitor.base.AppBaseActivity;
import com.lbn.monitor.util.DataUtils;
import com.lbn.monitor.widget.Appbar;
import com.lbn.monitor.widget.calendar.TimeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WHistoryActivity extends AppBaseActivity<WHistoryPresenter> implements
        IWHistoryView<List<RealTimeValue>>, BaseFragmentPagerAdapter.OnPageSelected {

    private static final String TAG = "IWHistoryActivity";

    private static final String STANDARD_VALUES = "STANDARD_VALUES";

    private static final String POLLUTANT = "POLLUTANT";

    private static final String DEVICE_MN = "DEVICE_MN";

    private static final String TIME = "TIME";

    private static final String DEVICE_NAME = "DEVICE_NAME";

    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;

//    @BindView(R.id.recycler_view)
//    RecyclerView recyclerView;
//    @BindView(R.id.ptr_frame)
//    PtrFrameLayout ptrFrame;

    private int pollutantId;

    private String pollutant;

    private String standard;

    private String unit;
//    private HistoryAdapter adapter;

    private String time;

    private String deviceMN;

    private String startTime, endTime;

    private int page = 1;

    private WHChartFragment chartFragment;
    private WHTableFragment tableFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iwhistory);
        ButterKnife.bind(this);
        setAppbar(new Appbar(this).openBack().setTitle("报警信息历史查询"));
    }

    @Override
    protected WHistoryPresenter createPresenter() {
        return new WHistoryPresenter();
    }

    @Override
    protected void onPresenterCreated() {
        super.onPresenterCreated();
        time = getIntent().getStringExtra(TIME);
        pollutant = getIntent().getStringExtra(POLLUTANT);
        standard = getIntent().getStringExtra(STANDARD_VALUES);
        deviceMN = getIntent().getStringExtra(DEVICE_MN);

        Log.e(TAG,"pollutant = " +pollutant);
        Log.e(TAG,"standard = " +standard);

        for (Pollutant p : DataUtils.getPollutants(this)) {
            if (p.getPollutantsFactor().equals(pollutant)) {
                pollutantId = Integer.parseInt(p.getId());
                unit = p.getUnit();
                break;
            }
        }
        Log.e(TAG,"pollutantId = " +pollutantId);

        startTime = TimeUtils.milliseconds2String(TimeUtils.string2Milliseconds(time), TimeUtils.DEFAULT_NOT_HH);
        endTime = TimeUtils.milliseconds2String(TimeUtils.string2Milliseconds(time), TimeUtils.DEFAULT_NOT_HH);

        tableFragment = WHTableFragment.newInstance();
        chartFragment = WHChartFragment.newInstance();
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(tableFragment);
        fragments.add(chartFragment);
        new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments, viewPager, radioGroup)
                .setOnPageSelected(this);
        chartFragment.setDevice(getIntent().getStringExtra(DEVICE_NAME));
//        adapter = new HistoryAdapter(this, recyclerView, null);
//        adapter.setPollutant(pollutant);
//        adapter.setPollutantId(pollutantId);
//        adapter.setStandard(standard);
//        adapter.setmOnLoadMoreListener(this);
////        adapter.setOnItemClickListener(this);
//        recyclerView.setAdapter(adapter);
//        initPtr(ptrFrame, this);
    }


    public static void startActivity(Context context, String deviceName, String DeviceMN, String pollutant, String standard, String time) {
        Intent intent = new Intent(context, WHistoryActivity.class);
        intent.putExtra(DEVICE_NAME, deviceName);
        intent.putExtra(DEVICE_MN, DeviceMN);
        intent.putExtra(STANDARD_VALUES, standard);
        intent.putExtra(POLLUTANT, pollutant);
        intent.putExtra(TIME, time);
        context.startActivity(intent);
    }

    @Override
    public void refreshComplete(List<RealTimeValue> data) {
//        adapter.refreshData(data,!(data==null||data.size()<5));
//        adapter.refreshData(data, !(data == null||data.size()<20));
//        ptrFrame.refreshComplete();
        chartFragment.refreshComplete(data);
        tableFragment.refreshComplete(data);
    }

    @Override
    public void addMoreData(List<RealTimeValue> data, boolean isMore) {
//        adapter.addMoreData(data,isMore);
        chartFragment.addMoreData(data, isMore);
        tableFragment.addMoreData(data, isMore);
    }


//    public void onRefresh() {
//        page = 1;
//        mPresenter.history(deviceMN, "2061", startTime, endTime + " 23:59:59", page);
//        chartFragment.showLoadingView(true);
//    }


    public void onRefresh() {
        page = 1;
        Log.e(TAG, "startTime" + startTime);
        Log.e(TAG, "endTime" + endTime);
        mPresenter.history(deviceMN, "2061", startTime, new String(endTime + " 23:59:59"), page);
//        chartFragment.showLoadingView(true);
    }


    public void onLoadMore() {
        page++;
        mPresenter.history(deviceMN, "2061", startTime, new String(endTime + " 23:59:59"), page);
//        chartFragment.showLoadingView(true);
    }

    public int getPollutantId() {
        return pollutantId;
    }

    public String getStandardValues() {
        return standard;
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        tableFragment.getPtrFrame().refreshComplete();
        LoadingUtils.dismiss(getSupportFragmentManager());
    }

    @Override
    public void onPageSelected(int position) {

        String title = "";

        switch (position) {
            case 0:
                title = "报警信息历史查询";
                break;
            case 1:
                title = pollutant+"-小时数据";
                break;
        }

        ((Appbar)mFjrAppbar).setTitle(title);
    }

    public String getWuran() {
        return pollutant;
    }

    public String getUnit() {
        return unit;
    }

}
